﻿package com.reyco1.util
{
	/*
	imports
	*/
	import com.reyco1.extension.StandardExtension
	import flash.external.ExternalInterface;
	import flash.utils.Dictionary;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	/*
	class
	*/
	public class QueryUtil extends StandardExtension
	{
		/*
		variables
		*/
		private var objDictionary:Dictionary
		private var strQueryString:String;
		private var strFullString:String;
		private var objParameters:URLVariables;
		private var objUrlRequest:URLRequest;
		/*
		constructor
		*/
		
		/**
		 * QueryUtil constructor
		 * 
		 */		
		public function QueryUtil()
		{
			this.objParameters = new URLVariables();
			this.initialize();
		}
		/*
		initialize function
		*/
				
		private function initialize():void
		{
			this.objDictionary = new Dictionary(true);
			this.strFullString = ExternalInterface.call("window.location.href.toString");
			this.strQueryString = ExternalInterface.call("window.location.search.substring", 1);
			
			var arrParams:Array = this.strQueryString.split("&");

			for (var a:uint = 0; a<arrParams.length; a++)
			{
				var arrTemp:Array = arrParams[a].split("=");
				this.objDictionary[arrTemp[0]] = arrTemp[1];
			}			
		}
		/*
		methods
		*/
		public function addParameters($parameters:Object, $url:String):URLRequest
		{
			var theURL:String = $url || this.url;
			this.objUrlRequest = new URLRequest($url);
			this.objParameters = $parameters as URLVariables;
			this.objUrlRequest.data = this.objParameters;
			return this.objUrlRequest;
		}
		
		/*
		properties
		*/
		
		/**
		 * Returns the value based on the provided key
		 
		 * @return Value string
		 * 
		 */
		public function getValue($key:String):String
		{
			return this.objDictionary[$key]
		}
		
		/**
		 * Returns only the query string
		 * @return 
		 * 
		 */		
		public function get queryString():String
		{
			return strQueryString;
		}
		
		/**
		 * Returns the full url
		 * @return 
		 * 
		 */		
		public function get url():String
		{
			return strFullString;
		}
		
		/**
		 * Reterns all the parameters in a key/value object
		 * @return 
		 * 
		 */		
		public function get parameters():Dictionary
		{
			return this.objDictionary;
		}
	}
}